import java.util.Scanner;

public class term2 {

    public static int maxSubArraySum(int[] nums) {
        if (nums == null || nums.length == 0) {
            return 0;
        }

        int maxSoFar = nums[0];
        int currMax = nums[0];

        for (int i = 1; i < nums.length; i++) {
            currMax = Math.max(nums[i], currMax + nums[i]);
            maxSoFar = Math.max(maxSoFar, currMax);
        }

        return maxSoFar;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入数组的长度: ");
        int length = scanner.nextInt();

        int[] nums = new int[length];

        System.out.println("请输入 " + length + " 个整数：");
        for (int i = 0; i < length; i++) {
            nums[i] = scanner.nextInt();
        }

        int maxSum = maxSubArraySum(nums);

        System.out.println("最大子数组和为: " + maxSum);

        scanner.close();
    }
}